//
//  LHQPlayerMusicTool.m
//  Music
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LHQPlayerMusicTool.h"

@implementation LHQPlayerMusicTool
+ (LHQPlayerMusicTool *)sharePlay{
    static LHQPlayerMusicTool *play;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        play = [[LHQPlayerMusicTool alloc] init];
        play.player = [[AVPlayer alloc] init];
        
    });
    return play;
}

+(void)getPlay:(NSString *)urlStr{
    
    //判断是否重复的歌曲
    if([[LHQPlayerMusicTool sharePlay].isRep isEqualToString:urlStr]) return;
    
    [LHQPlayerMusicTool sharePlay].isRep = urlStr;
    // 转码
    NSString * str = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //播放列表
    AVPlayerItem *playerItem =[AVPlayerItem playerItemWithURL:[NSURL URLWithString:str]];
    [LHQPlayerMusicTool sharePlay].player = [AVPlayer playerWithPlayerItem:playerItem];
    //播放歌曲
    [[LHQPlayerMusicTool sharePlay].player play];
    [LHQPlayerMusicTool sharePlay].isplay = YES;
    //播放完成的监听
    [[LHQPlayerMusicTool sharePlay] addNot];
    [[LHQPlayerMusicTool sharePlay] addProgress];
}
-(void)addProgress{
    
    AVPlayerItem *playerItem=self.player.currentItem;
    //这里设置每秒执行一次
    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        CGFloat current=CMTimeGetSeconds(time);
        CGFloat total=CMTimeGetSeconds([playerItem duration]);
        //        NSLog(@"当前已经播放%.2fs.",current);
        
        if (current) {
            [LHQPlayerMusicTool sharePlay].pross = [NSString stringWithFormat:@"%f",(current / total)];
            
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[LHQPlayerMusicTool sharePlay].pross,@"pross", nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"pross" object:nil userInfo:dic];
        }
    }];
    
}

//播放完毕通知
- (void)addNot{
    [[NSNotificationCenter defaultCenter] addObserver:[LHQPlayerMusicTool sharePlay] selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:[LHQPlayerMusicTool sharePlay].player.currentItem];
}
-(void)playbackFinished:(NSNotification *)notification{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"end" object:nil];
    [LHQPlayerMusicTool sharePlay].isEnd = YES;
}
//播放本地音乐
+ (void)getLocal:(NSString *) urlStr{
    
    //判断是否重复的歌曲
    if([[LHQPlayerMusicTool sharePlay].isRep isEqualToString:urlStr]) return;
    
    [LHQPlayerMusicTool sharePlay].isRep = urlStr;
    
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:urlStr]];
    
    [[LHQPlayerMusicTool sharePlay].player replaceCurrentItemWithPlayerItem:playerItem];
    
    //播放歌曲
    [[LHQPlayerMusicTool sharePlay].player play];
    [LHQPlayerMusicTool sharePlay].isplay = YES;
    
    //播放完成的监听
    [[LHQPlayerMusicTool sharePlay] addProgress];
    
}

//单曲播放
+ (void)getSinglePlay:(NSString *) urlStr{
    
    // 转码
    NSString * str = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //播放列表
    AVPlayerItem *playerItem =[AVPlayerItem playerItemWithURL:[NSURL URLWithString:str]];
    [LHQPlayerMusicTool sharePlay].player = [AVPlayer playerWithPlayerItem:playerItem];
    //播放歌曲
    [[LHQPlayerMusicTool sharePlay].player play];
    [LHQPlayerMusicTool sharePlay].isplay = YES;
    //播放完成的监听
    [[LHQPlayerMusicTool sharePlay] addNot];
    [[LHQPlayerMusicTool sharePlay] addProgress];
    
}


@end
